/**
 *  helpers.cpp
 *
 *  Copyright (C) 2008  David Andrs <pda@jasnapaka.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <afxwin.h>
#include "helpers.h"

#include "../share/UIHelper.h"
#include "md5/global.h"
#include "md5/MD5.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


//////////////////////////////////////////////////////////////////////////////
// FUNCTION: InWideMode()
//
BOOL InWideMode() {
	int width = GetSystemMetrics(SM_CXSCREEN);
	int height = GetSystemMetrics(SM_CYSCREEN);
	return (width > height && height < SCALEX(320)) ? TRUE : FALSE;
}


//
//
//

char *WCharToChar(const CString &str) {
	int len = str.GetLength();
	char *conv = new char [len + 1];
	for (int i = 0; i < len; i++)
		conv[i] = (char) str.GetAt(i);
	conv[len] = '\0';
	return conv;
}

char *WCharToChar(const CString &str, UINT codePage) {
	int len = WideCharToMultiByte(codePage, 0, str, str.GetLength(), NULL, 0, NULL, NULL);
	char *conv = new char [len + 1];
	WideCharToMultiByte(codePage, 0, str, str.GetLength(), conv, len, NULL, NULL);
	conv[len] = '\0';
	return conv;
}

CString CharToWChar(const char *str, UINT codePage) {
	int wLen = MultiByteToWideChar(codePage, 0, str, -1, NULL, 0);
	CString s;
	LPTSTR wStr = s.GetBufferSetLength(wLen - 1);
	MultiByteToWideChar(codePage, 0, str, -1, wStr, wLen - 1);
	return s;
}

CString CharToWChar(const char *str) {
	int len = strlen(str);
	CString s;
	LPTSTR sStr = s.GetBufferSetLength(len);
	for (int i = 0; i < len; i++)
		s.SetAt(i, (TCHAR) str[i]);

	return s;
}

CString CharToWChar(const char *str, int length, UINT codepage/* = CP_ACP*/) {
	int wLen = MultiByteToWideChar(codepage, 0, str, length, NULL, 0);
	CString s;
	TCHAR *wStr = s.GetBufferSetLength(wLen);
	MultiByteToWideChar(codepage, 0, str, length, wStr, wLen);
	return s;

/*	TCHAR *wStr = new TCHAR [wLen + 1];
	MultiByteToWideChar(codepage, 0, str, length, wStr, wLen);
	wStr[wLen] = '\0';
	CString s;
	s.Format(_T("%s"), wStr);
	delete [] wStr;
	return s;
*/
}

#define HASHLEN 16
#define HASHHEXLEN 32

typedef unsigned char HASH[HASHLEN];
typedef unsigned char HASHHEX[HASHHEXLEN+1];

CString CalculateMD5(const CString &str) {
	MD5_CTX md5ctx;
	HASH ha1;

	char *buffer = WCharToChar(str, CP_ACP);

	MD5Init(&md5ctx);
	MD5Update(&md5ctx, (unsigned char *) buffer, strlen(buffer));
	MD5Final(ha1, &md5ctx);

	delete [] buffer;

	// convert to hex
	HASHHEX hex;
	for (int i = 0; i < HASHLEN; i++) {
		int j;

		j = (ha1[i] >> 4) & 0xf;
		if (j <= 9)
			hex[i*2] = (j + '0');
		else
			hex[i*2] = (j + 'a' - 10);
		j = ha1[i] & 0xf;
		if (j <= 9)
			hex[i*2+1] = (j + '0');
		else
			hex[i*2+1] = (j + 'a' - 10);
	};
	hex[HASHHEXLEN] = '\0';

	return CharToWChar((const char *) hex + 0);
}


CString DecodeURL(const CString &url) {
	CString str = url;

	int pos, start;
	pos = str.Find('%');
	while (pos != -1) {
		CString e = str.Mid(pos + 1, 2);
		TCHAR ch;
		swscanf(e, _T("%x"), &ch);
		str = str.Left(pos) + CString(ch) + str.Mid(pos + 3);

		start = pos + 1;
		pos = str.Find('%', start);
	}

	str.Replace('+', ' ');

	return str;
}
